package com.tiankong.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.tiankong.product.entity.Attr;
import com.tiankong.product.entity.AttrAttrgroup;
import com.tiankong.product.entity.AttrGroup;
import com.tiankong.product.mapper.AttrGroupMapper;
import com.tiankong.product.service.AttrAttrgroupService;
import com.tiankong.product.service.AttrGroupService;
import net.youdi.common.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 属性分组 服务实现类
 * </p>
 *
 * @author scl
 * @since 2021-03-22
 */
@Service
public class AttrGroupServiceImpl extends BaseServiceImpl<AttrGroupMapper, AttrGroup> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupService attrRelationService;

    @Override
    public void batchDelete(Set<Integer> ids) {
        removeByIds(ids);
        attrRelationService.removeRelationGroup(ids);
    }

    @Override
    public List<AttrGroup> listWithAttrByCatId(Long catelogId) {
        List<AttrGroup> attrGroups = query().eq("catelog_id", catelogId).list();
        if (CollectionUtil.isNotEmpty(attrGroups)){
            for (AttrGroup attrGroup : attrGroups) {
                List<Attr> attrs = attrRelationService.getRelationAttr(attrGroup.getAttrGroupId());
                attrGroup.setAttrs(attrs);
            }
        }
        return attrGroups;
    }
}
